﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLT_BLL;
using QLT_Public;

namespace GiaoDien
{
    public partial class frm_HoaDonBH_KH : DevExpress.XtraEditors.XtraForm
    {
        public frm_HoaDonBH_KH()
        {
            InitializeComponent();
        }
        KhachHang_BLL khachHang_bll = new KhachHang_BLL();
        private void frm_HoaDonBH_KH_Load(object sender, EventArgs e)
        {
            gridControl1.DataSource = khachHang_bll.LayDuLieu();
            txtLoad.Hide();
        }
        public delegate void GetInt(int ma);
        public GetInt GetData;
        private void gridControl1_DoubleClick(object sender, EventArgs e)
        {
            GetData(Convert.ToInt32(gridView1.GetRowCellValue(gridView1.FocusedRowHandle,"Mã Khách Hàng")));
            this.Close();
        }

        private void btnChon_Click(object sender, EventArgs e)
        {
            GetData(Convert.ToInt32(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Mã Khách Hàng")));
            this.Close();
        }

        private void btn_Them_Click(object sender, EventArgs e)
        {
            frm_HoaDonBH_KH_Them f = new frm_HoaDonBH_KH_Them();
            f.Flag = 0;
            f.MyGetLoad = new frm_HoaDonBH_KH_Them.GetInt(GetLoadForm);
            f.ShowDialog();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            frm_HoaDonBH_KH_Them f = new frm_HoaDonBH_KH_Them();
            f.Flag = 1;
            f.MyGetLoad = new frm_HoaDonBH_KH_Them.GetInt(GetLoadForm);
            f.MaKH = Convert.ToInt32(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Mã Khách Hàng"));
            f.TenKH = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Tên Khách Hàng").ToString();
            f.DiaChi = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Địa Chỉ").ToString();
            f.ShowDialog();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            KhachHang_Public kh = new KhachHang_Public();
            kh.MaKH = Convert.ToInt32(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Mã Khách Hàng"));
            kh.TenKH=gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Tên Khách Hàng").ToString();
            if (XtraMessageBox.Show("Bạn có muốn xóa Khách hàng: '" + kh.TenKH + "' có Mã là: '" + kh.MaKH + "' này không?", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                khachHang_bll.delete_KhachHang(kh);
                frm_HoaDonBH_KH_Load(sender,e);
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        public void GetLoadForm(int kt)
        {
            txtLoad.Text = kt.ToString();
        }
        private void txtLoad_TextChanged(object sender, EventArgs e)
        {
            if (txtLoad.Text == "1")
            {
                frm_HoaDonBH_KH_Load(sender,e);
                txtLoad.Text = "0";
            }
        }
    }
}